import {Iaction} from '../../utils/interface';
const initialState = {
    cartList:[],
    cartTotal:{}
}
const reducer = (state:any,action:Iaction)=>{
    switch(action.type){
        case 'INIT_CART' :
            let {cartTotal,cartList} = action.payload;
            cartList = cartList&&cartList.map((item:{[key:string]:any})=>{
                return {...item,delChecked:false}
            })
            return {...state,cartTotal,cartList}
        case 'UPDATE_CART' :
            return {...state,...action.payload}
        case 'CHECKED_CART' :
            return {...state,...action.payload}
        case 'DEL_GOODS' :
            return {...state,...action.payload}
        case 'UPDATE_FLAG' :
            return {...state,...action.payload}
        case 'CHANGE_FLAG' :
            const {id} = action.payload;
            state.cartList.forEach((v: { [key: string]: any }) => {
                if (v.id === id){
                    v.delChecked = !v.delChecked
                }
            })
            return {...state,cartList:[...state.cartList]}
        case 'EDIT_ALL_FLAG' :
            state.cartList.forEach((v: { [key: string]: any }) => {
                v.delChecked = !v.delChecked
            })
            return {...state,cartList:[...state.cartList]}
        case 'INIT_FLAG' :
            state.cartList.forEach((v: { [key: string]: any }) => {
                v.delChecked = false
            })
            return {...state,cartList:[...state.cartList]}
        default :
            return state
    }
}

export default (state:any=initialState,action:Iaction)=>reducer(state,action)